$(document).ready(function() {

    // Language menu hover
    $('#top ul li').hoverIntent(
        function () {
            $(this).children('ul').show('fast');
        },
        function () {
            $(this).children('ul').hide('fast');
        }
        );
            

    // Language menu hover
    $('.how-to li').hoverIntent(
        function(){
            $(this).children('h4').css('text-decoration','underline');
        },
        function(){
            $(this).children('h4').css('text-decoration','none');
        }
        );

    // Footer hover and click
    $('.how-to li').live('click',function(){

        window.location.href = $(this).children('h4').children('a').attr('href');

    })

    // Mail list
    var formToSubmit = "#maillist-form"; 
    var formbutton = "#maillist_submit"; 
    jQuery.validator.messages.required = "";

    // Override generation of error label
    jQuery(formToSubmit).validate({
        rules: {
            maillist_email: {
                required: true,
                email: true
            }
        },
        submitHandler: function(form) {
            //var that = jQuery('#form-submit-btn').busy({ position : 'left', hide : false });
            jQuery(formbutton).attr('disabled','disabled');
            $(formbutton).busy({
                img:base_url+'assets/img/busy2.gif'
            });
            var form = jQuery(formToSubmit);
            var form_data = form.serialize();

            jQuery.ajax({
                url: form.attr('action'),
                dataType: 'json',
                type: "POST",
                data: form_data,
                success: function (data, textStatus, xhr) {

                    if(data.action){
                        alert(data.msg);
                    }else{
                        alert(data.msg);                            
                    }
                    $(formbutton).busy('hide');
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert('Error ocured...\n Server not found.');
                    jQuery(formbutton).removeAttr('disabled');
                    $(formbutton).busy('hide');
                }
            });

            return false;

        },
        errorPlacement: function(error, element){}


    });

    jQuery(formbutton).removeAttr('disabled');

    // contact idle handler
    jQuery('#maillist_email').live('focus', function() {
        if(jQuery(this).val()=='Your e-mail adress...'){
            jQuery(this).val('');
            jQuery(this).removeClass('idle');
        }
    });
    jQuery('#maillist_email').live('blur', function() {

        if(jQuery(this).val()==''){
            jQuery(this).addClass('idle');
            jQuery(this).val('Your e-mail adress...');
        }

    });

    show_page = function(id)
    {
        $.ajax({
            url: base_url+lang+'/page/popup/'+id,
            dataType: 'json',
            type: "POST",
            success: function (data, textStatus, xhr) {

                if(data.action){
                    $('#bpopup_page').html(data.html);
                    jQuery('#bpopup_page').bPopup({}, 
                        function() {

                        //initialzie when popup is visible
                        });
                }else{
                    alert(data.msg);                            
                }
            },
            error: function (xhr, textStatus, errorThrown) {

                alert('Method on server not found.');

            }
        });
    }

    // show popup 
    $('.show_popup').live('click',function(){
        show_page($(this).attr('data-holder'));
        return false;
    });

    $('.close_popup').live('click',function(){
        $('.bClose').trigger('click');
    })   
    
    // Tabs
    $('.tabs').tabs({
        fx:[
        {
            opacity:'toggle'
        },

        {
            opacity:'toggle'
        }
        ],
		load: function() {
			setTimeout(function() { $('.home-slider').find('.info p').trunk8({lines:2}); }, 50);
		},
		hide:function() {
			setTimeout(function() { $('.home-slider').find('.info p').trunk8({lines:2}); }, 50);
		},
		select: function() {
			$(this).find('.info p').trunk8({lines:2});
		},
		show: function() {
			$(this).find('.info p').trunk8({lines:2});
		}
    }).tabs('rotate', 7000, true);
	$('.onenews > p').trunk8({ lines:3, tooltip:false });
});

$(window).load(function() {
	$('.home-slider').find('.info p').trunk8({lines:2});
	$('.onenews > p').trunk8({ lines:3, tooltip:false });
});